//
//  XZEmotionKeyboard.m
//  XZIBeeBo
//
//  Created by Sean on 16/4/14.
//  Copyright © 2016年 XZSean. All rights reserved.
//

#import "XZEmotionKeyboard.h"
#import "XZEmotionTabBar.h"
#import "XZEmotionListView.h"
#import "XZEmotion.h"
#import "MJExtension.h"


@interface XZEmotionKeyboard()<XZEmotionTabBarDelegaet>

/** 表情内容 */
//@property (nonatomic, weak) XZEmotionListView *listView;
/** 保存正在显示listView */
@property (nonatomic, weak) XZEmotionListView *showingListView;

/** 表情内容 */
@property (nonatomic, strong) XZEmotionListView *recentListView;
@property (nonatomic, strong) XZEmotionListView *defaultListView;
@property (nonatomic, strong) XZEmotionListView *emojiListView;
@property (nonatomic, strong) XZEmotionListView *lxhListView;
/** tabbar */
@property (nonatomic, weak) XZEmotionTabBar *tabBar;

@end

@implementation XZEmotionKeyboard
#pragma mark---懒加载
- (XZEmotionListView *)recentListView{
  
    if (!_recentListView) {
        self.recentListView = [[XZEmotionListView alloc] init];
        
    }
    return _recentListView;
}
-(XZEmotionListView *)defaultListView{

    if (!_defaultListView) {
        self.defaultListView = [[XZEmotionListView alloc] init];
         //取出沙盒中的表情数据
        NSArray *defaultArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"EmotionIcons/default/info.plist" ofType:nil]];
        //bjectArrayWithKeyValuesArray:转换成模型,面向模型开发      //然后赋值给数组
        self.defaultListView.emotions = [XZEmotion mj_objectArrayWithKeyValuesArray:defaultArray];
    }
    return _defaultListView;
}

-(XZEmotionListView *)emojiListView{
 
    if (!_emojiListView) {
        self.emojiListView = [[XZEmotionListView alloc] init];
        NSArray *emojiArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"EmotionIcons/emoji/info.plist" ofType:nil]];
        self.emojiListView.emotions = [XZEmotion mj_objectArrayWithKeyValuesArray:emojiArray];
        
    }
    return _emojiListView;
}
-(XZEmotionListView *)lxhListView{

    if (!_lxhListView) {
        self.lxhListView = [[XZEmotionListView alloc] init];
        NSArray *lxhArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"EmotionIcons/lxh/info.plist" ofType:nil]];
        self.lxhListView.emotions = [XZEmotion mj_objectArrayWithKeyValuesArray:lxhArray];
    }
    return _lxhListView;
}



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        // 1.表情内容
//        XZEmotionListView *listView = [[XZEmotionListView alloc] init];
//      // listView.backgroundColor = XZRandomColor;
//        [self addSubview:listView];
//        //self.listView = listView;
        
        // 2.tabbar
        XZEmotionTabBar *tabBar = [[XZEmotionTabBar alloc] init];
       // tabBar.backgroundColor = XZRandomColor;
        [self addSubview:tabBar];
        tabBar.delegate = self;
        self.tabBar = tabBar;
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 1.tabbar
    self.tabBar.width = self.width;
    self.tabBar.height = 37;//和图片的大小一样
    self.tabBar.x = 0;
    self.tabBar.y = self.height - self.tabBar.height;
    
    // 2.表情内容
    self.showingListView.x = self.showingListView.y = 0;
    self.showingListView.width = self.width;
    self.showingListView.height = self.tabBar.y;
}

#pragma mark----XZEmotionTabBarDelegaet
- (void)emotionTabBar:(XZEmotionTabBar *)tabBar didSelectButton:(XZEmotionTabBarButtonType)buttonType{
    // 移除正在显示的listView控件
    [self.showingListView removeFromSuperview];
    
    //添加表情数据
    switch (buttonType) {
        case XZEmotionTabBarButtonTypeRecent: //Recent
            [self addSubview:self.recentListView];
            XZLog(@"Recent");
            break;
        case XZEmotionTabBarButtonTypeDefault:{ //默认
           [self addSubview:self.defaultListView];
       
            
        }
            break;
        case XZEmotionTabBarButtonTypeEmoji:{ //Emoji
            [self addSubview:self.emojiListView];
            
        }
            break;
        case XZEmotionTabBarButtonTypeLxh:{//浪小花
            [self addSubview:self.lxhListView];
        }
            break;
        
    }
    // 设置正在显示的listView
    self.showingListView = [self.subviews lastObject];
    
    // 设置frame强制刷新layoutSubviews 千万不要再次调用layoutSubviews
    [self setNeedsLayout];
}



@end
